package com.wechat.department;

import io.restassured.response.Response;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Demo_02_separt {
    private static final Logger logger = LoggerFactory.getLogger(Demo_02_separt.class);
    static String accessToken;
    static String departmentId;

    @BeforeAll
    @DisplayName("获取Token" )
    public static  void getAccessToken(){


        accessToken=given().log().all()
                .when()
                .param("corpid","wwa8875fa952a66c97")
                .param("corpsecret","T3a5cQBQLSk5GQyt6w3-IBI890gv8hrhtlbKLtVaAnE")
                .get("https://qyapi.weixin.qq.com/cgi-bin/gettoken")
                .then().log().all()
                .extract().response().path("access_token");
        logger.info(accessToken);

    }

    @DisplayName("创建部门")
    @Test
    @Order(1)
    void creatDepartment(){
        String body="{\n" +
                "   \"name\": \"北京研发中心\",\n" +
                "   \"name_en\": \"RDG4\",\n" +
                "   \"parentid\": 1\n" +
                "}";
        final Response response=given().log().all()
                .when()
                .contentType("application/json;charset=UTF-8")
                .body(body)
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token="+accessToken)
                .then().log().all()
                .extract().response();
        departmentId=response.path("id").toString();
        logger.info("部门ID："+departmentId);
        assertEquals("0",response.path("errcode").toString());
        assertEquals("created",response.path("errmsg"));
    }
    @DisplayName("更新部门")
    @Test
    @Order(2)
    public void updateDepartment(){
        String body="{\n" +
                "    \"id\": "+departmentId+",\n"+
                "    \"name\": \"太原研发中心\",\n" +
                "    \"name_en\": \"222\"\n" +
                "}";
        Response response1=given().log().all()
                .when()
                .contentType("application/json;charset=UTF-8")
                .body(body)
                .post("https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token="+accessToken)
                .then().log().all()
                .extract().response();
        assertEquals("0",response1.path("errcode").toString());
        assertEquals("updated",response1.path("errmsg"));

    }
    @DisplayName("删除部门")
    @Test
    @Order(4)
    public void deleteDepartment(){

        Response response2=given().log().all()
                .when()
                .contentType("application/json;charset=UTF-8")
                .param("access_token",accessToken)
                .param("id",departmentId)
                .get("https://qyapi.weixin.qq.com/cgi-bin/department/delete")
                .then().log().all()
                .extract().response();
        assertEquals("0",response2.path("errcode").toString());
        assertEquals("deleted",response2.path("errmsg"));


    }
    @DisplayName("获取部门列表")
    @Test
    @Order(3)
    public void searchDepartment(){

        Response response3=given().log().all()
                .when()
                .contentType("application/json;charset=UTF-8")
                .param("access_token",accessToken)
                .get("https://qyapi.weixin.qq.com/cgi-bin/department/list")
                .then().log().all()
                .extract().response();
        assertEquals("0",response3.path("errcode").toString());
        assertEquals("ok",response3.path("errmsg"));


    }

}
